package com.lancoo.ccas52.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lancoo.ccas52.entity.Major;
import com.lancoo.ccas52.entity.PlanTeaching;
import com.lancoo.ccas52.mapper.MajorMapper;
import com.lancoo.ccas52.mapper.RoomMapper;
import com.lancoo.ccas52.pojo.response.basicPlatform.BaseCollege;
import com.lancoo.ccas52.pojo.vo.Campus;
import com.lancoo.ccas52.pojo.vo.CollegeDto;
import com.lancoo.ccas52.service.MajorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author quin.song
 * @since 2023-01-03
 */
@Service
public class MajorServiceImpl extends ServiceImpl<MajorMapper, Major> implements MajorService {

    @Resource
    private MajorMapper majorMapper;
    @Resource
    private RoomMapper roomMapper;

    @Override
    public void syncMajor(Integer taskId,String schoolId,String term, Set<Major> syncMajor) {
        List<Major> insertList = new ArrayList<>();
        //获取数据库专业
        List<Major> majorList = this.list(new LambdaQueryWrapper<Major>().eq(Major::getTaskId, taskId));
        for (Major major : syncMajor) {
            if (!majorList.contains(major)) {
                insertList.add(major);
            }
        }

        //填充默认校区
        List<Campus> campusList = roomMapper.getCampusList(schoolId, term);
        if (CollUtil.isNotEmpty(campusList)) {
            insertList.forEach(o -> {
                o.setCampusId(campusList.get(0).getCampusId());
                o.setCampusName(campusList.get(0).getCampusName());

            });
        }

        majorList.removeAll(syncMajor);
        List<Long> deleteIds = majorList.stream().map(Major::getMajorId).collect(Collectors.toList());
        if (CollUtil.isNotEmpty(deleteIds)) {
            this.removeBatchByIds(deleteIds);
        }
        if (CollUtil.isNotEmpty(insertList)) {
            majorMapper.addBatch(insertList);
        }

    }


}
